package com.aaa.controller;

import com.aaa.unilt.UploadUnilt;
import com.aaa.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/9/26
 */
@Controller
public class TestInterceptor {
    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取上传的路径
        String path = request.getSession().getServletContext().getRealPath("upload");
        File target=new File(path);
        //判断文件夹是否存在
        if(!target.exists()){
            target.mkdirs();//创建文件夹
        }

        //把上面myfile 转移到 target目录下。 IO
        System.out.println(file.getOriginalFilename());
        String filename = UUID.randomUUID()+file.getOriginalFilename();
        File file2=new File(path+"/"+filename);
        file.transferTo(file2);

        return "index.jsp";
    }
    //springmvc把上传的文件也封装了一个类对象，参数的名称必须和前端的名称一致
    @RequestMapping("/upload02")
    @ResponseBody
    public Result upload02(MultipartFile file, HttpServletRequest request)throws Exception{
        String path = UploadUnilt.upload(file, request);
        return new Result(200,"上传成功",path);
    }
    @RequestMapping("/upload03")
    @ResponseBody
    public Result upload03(MultipartFile file)throws Exception{
        String path = UploadUnilt.ossUpload(file);
        return new Result(200,"上传成功",path);
    }
}
